package cn.gogoyc.order.controller;

import cn.gogoyc.order.service.IOrderPetAcquisitionService;
import cn.gogoyc.order.domain.OrderPetAcquisition;
import cn.gogoyc.order.query.OrderPetAcquisitionQuery;
import cn.gogoyc.org.basic.util.AjaxResult;
import cn.gogoyc.org.basic.util.PageList;
import cn.gogoyc.pet.dto.OrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {

    @Autowired
    public IOrderPetAcquisitionService orderPetAcquisitionService;

    /**
     * 接口：添加或修改
     *
     * @param orderPetAcquisition 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition) {
        try {
            if (orderPetAcquisition.getId() != null) {
                orderPetAcquisitionService.update(orderPetAcquisition);
            } else {
                orderPetAcquisitionService.save(orderPetAcquisition);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败", false);
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            orderPetAcquisitionService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败", false);
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            orderPetAcquisitionService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("批量删除失败", false);
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public OrderPetAcquisition findOne(@PathVariable("id") Long id) {
        return orderPetAcquisitionService.loadById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<OrderPetAcquisition> findAll() {
        return orderPetAcquisitionService.loadAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query,
                                                   HttpServletRequest request) {
        return orderPetAcquisitionService.queryPage(query);
    }

    @PutMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable("id") Long id) {
        try {
            orderPetAcquisitionService.cancel(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络繁忙...请稍后再试....", false);
        }
    }

    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderDto orderDto) {
        try {
            orderPetAcquisitionService.confirm(orderDto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("网络繁忙...请稍后再试....", false);
        }
    }
}
